{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![alt text](https://github.com/callysto/callysto-sample-notebooks/blob/master/notebooks/images/Callysto_Notebook-Banner_Top_06.06.18.jpg?raw=true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Climate change"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%html\n",
    "\n",
    "<script>\n",
    "  function code_toggle() {\n",
    "    if (code_shown){\n",
    "      $('div.input').hide('500');\n",
    "      $('#toggleButton').val('Show Code')\n",
    "    } else {\n",
    "      $('div.input').show('500');\n",
    "      $('#toggleButton').val('Hide Code')\n",
    "    }\n",
    "    code_shown = !code_shown\n",
    "  }\n",
    "\n",
    "  $( document ).ready(function(){\n",
    "    code_shown=false;\n",
    "    $('div.input').hide()\n",
    "  });\n",
    "</script>\n",
    "<form action=\"javascript:code_toggle()\"><input type=\"submit\" id=\"toggleButton\" value=\"Show Code\"></form>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## What do we know?\n",
    "\n",
    "The Earth's climate has been subject to changes throughout history and most of these can be attributed to variations in Earth's orbit resulting in the amount of solar energy that is received. The reason behind significant increase in warming trend since the industrial revolution is extremely likely to be the result of human activity.\n",
    "\n",
    "Earth is a complex intergrated system of components and processes. A disturbance in one component can have unprecedented and irreversible effects over another component. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## How do we know it?\n",
    "\n",
    "Earth-orbiting satellites and other scientific advancements have made it possible to understand Earth on a global scale and have enabled scientists to collect many different types of information about our planet, its climate and use it to analyze how it has changed over the years. \n",
    "\n",
    "It is important to understand the evidence available for global climate change and the potential impacts it can cause in order to address the challenges that we may face. Knowing how these Earth systems have changed in the past and how they are changing in response to climate change now will help us understand how they will likely change in the future.\n",
    "\n",
    "Scientists gather data on global temperature, Carbon dioxide in the atmosphere, Arctic sea ice, land ice and sea level on a regular basis to analyze and understand how climate is changing over the years."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Evidence for rapid climate change"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Evidence 1 : Global Land-Ocean Termperature index\n",
    "\n",
    "\n",
    "Data Source & Inspiration: NASA Global Climate Change, https://climate.nasa.gov/vital-signs/global-temperature/\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "An increase in carbon dioxide and other human-made emissions into the atmosphere has caused our planet's average surface temperature to rise by 1.1 degrees celsius since the late 19th century."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Below you can see a visualization created by NASA's scientific visualization studio showing the Global temperature anomalies from 1880 to 2017."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import YouTubeVideo\n",
    "YouTubeVideo('xlrFFiSROmg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import matplotlib as mpl\n",
    "import matplotlib.pyplot as plt\n",
    "from plotly.offline import init_notebook_mode, iplot\n",
    "import plotly.graph_objs as go\n",
    "from plotly.graph_objs import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "url = 'https://climate.nasa.gov/system/internal_resources/details/original/647_Global_Temperature_Data_File.txt'\n",
    "\n",
    "global_temperature_df = pd.read_table(url, sep='\\s+', header=None, names=['Year','Annual Mean', 'Lowess smoothing'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "global_temperature_df.tail(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "init_notebook_mode(connected=True)\n",
    "\n",
    "trace0 = go.Scatter(\n",
    "    x = global_temperature_df['Year'],\n",
    "    y = global_temperature_df['Annual Mean'],\n",
    "    mode = 'lines+markers',\n",
    "    name = 'Annual Mean'\n",
    ")\n",
    "\n",
    "trace1 = go.Scatter(\n",
    "    x = global_temperature_df['Year'],\n",
    "    y = global_temperature_df['Lowess smoothing'],\n",
    "    mode = 'lines+markers',\n",
    "    name = 'Lowess smoothing'\n",
    ")\n",
    "\n",
    "data = [trace0,trace1]\n",
    "layout = go.Layout(dict(title='Global Land Ocean Temperature Index'),\n",
    "                   xaxis=dict(title='Year'),\n",
    "                   yaxis=dict(title='Temperature Anomaly')\n",
    ")\n",
    "fig = go.Figure(data=data,layout=layout)\n",
    "iplot(fig)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This graph illustrates the change in global surface temperature relative to 1951-1980 average temperatures. Seventeen of the 18 warmest years in the 136-year record all have occurred since 2001, with the exception of 1998. The year 2016 ranks as the warmest on record. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By looking at the graph above and the video one can see that 2016 was the warmest year since modern record keeping began."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Evidence 2: Decline in extent and thickness of Arctic Sea Ice"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Scientists keep a close eye on Arctic sea ice as it warms through the years and it is found to be shrinking by about 13.2% per decade, relative to the 1981 to 2010 average.\n",
    "\n",
    "Sea ice area and extent are two different measuers and they provide different information. Sea ice extent is a measure of edges of sea ice and all of the space inside it, while sea ice area is a measure of where there is ice only, not including the areas that doesn't have ice. Extent is always a larger number than area when compared at the same period of time.\n",
    "\n",
    "Decrease in both sea ice extent and area impacts a wide variety of marine lives that sustain in the Arctic right from single-celled algae to bowhead whales, polar bears and other mammals not excluding the indigenous people who rely on sea ice for transportation and hunting."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The data used below is curated by NASA's global climate change intiative, originally pulled from National Snow and Ice Data Center."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "url = 'https://climate.nasa.gov/system/internal_resources/details/original/1270_minimum_extents_and_area_north_SBA_reg_20171001_2_.txt'\n",
    "\n",
    "arctic_ice_df = pd.read_table(url, sep='\\s+', skiprows=[0,1,2,3],header=None, names=['Year', 'MN', 'DY', 'Ice Extent', 'Year1', 'MN1', 'DY1', 'Ice Area'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "arctic_ice_df.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "init_notebook_mode(connected=True)\n",
    "\n",
    "trace0 = go.Scatter(\n",
    "    x = arctic_ice_df['Year'],\n",
    "    y = arctic_ice_df['Ice Extent'],\n",
    "    mode = 'lines+markers',\n",
    "    name = 'Ice Extent'\n",
    ")\n",
    "\n",
    "trace1 = go.Scatter(\n",
    "x = arctic_ice_df['Year'],\n",
    "y = arctic_ice_df['Ice Area'],\n",
    "mode = 'lines+markers',\n",
    "#opacity = 0.5,\n",
    "name='Ice Area',\n",
    "#hoverinfo = 'none'\n",
    ")\n",
    "data = [trace0, trace1]\n",
    "layout = go.Layout(dict(title='Arctic Sea Ice Index'),\n",
    "                   xaxis=dict(title='Year'),\n",
    "                   yaxis=dict(title='Million Square Kms')\n",
    ")\n",
    "fig = go.Figure(data=data,layout=layout)\n",
    "iplot(fig)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The image below represents sea ice concetration data from NASA National Snow & Ice Data Center's distributed Active Archive center from April 2018.\n",
    "\n",
    "*Data Source: Maslanik, J. and J. Stroeve. 1999, updated daily. Near-Real-Time DMSP SSMIS Daily Polar Gridded Sea Ice Concentrations, Version 1. Boulder, Colorado USA. NASA National Snow and Ice Data Center Distributed Active Archive Center. doi: https://doi.org/10.5067/U8C09DWVX9LM. [April 2018]*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import numpy.ma as ma\n",
    "\n",
    "sea_ice = ['./data/nt_20150418_f17_nrt_n.bin','./data/nt_20160331_f17_nrt_n.bin','./data/nt_20170418_f18_nrt_n.bin','./data/nt_20180423_f18_nrt_n.bin']\n",
    "\n",
    "def prepare_data(ice_data):\n",
    "    with open(ice_data, 'rb') as fr:\n",
    "        hdr = fr.read(300)\n",
    "        ice = np.fromfile(fr, dtype=np.uint8)\n",
    "\n",
    "    ice = ice.reshape(448, 304)\n",
    "\n",
    "    ice = ice / 250.\n",
    "\n",
    "    ice = ma.masked_greater(ice, 1.0)\n",
    "    dx = dy = 25000\n",
    "    x = np.arange(-3850000, +3750000, +dx)\n",
    "    y = np.arange(+5850000, -5350000, -dy)\n",
    "    return x, y, ice"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "import cartopy.crs as ccrs\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.animation as animation\n",
    "\n",
    "fig = plt.figure(figsize=(9,9))\n",
    "ax = plt.axes(projection=ccrs.NorthPolarStereo(central_longitude=0))\n",
    "i=2015\n",
    "for ice_data in sea_ice:\n",
    "    x, y, ice = prepare_data(ice_data)\n",
    "    cs = ax.coastlines(resolution='110m', linewidth=0.5)\n",
    "    ax.gridlines()\n",
    "    ax.set_extent([-180, 180, 40, 90], crs=ccrs.PlateCarree())\n",
    "    kw = dict(central_latitude=90, central_longitude=-45, true_scale_latitude=70)\n",
    "    cs = ax.pcolormesh(x, y, ice, cmap=plt.cm.Blues,\n",
    "                   transform=ccrs.Stereographic(**kw))\n",
    "    fig.suptitle(\"April %s\" %(i))\n",
    "    fig.savefig(f'frames/frame_{i}.png', dpi=100, frameon=False, facecolor='white')\n",
    "    i+=1   \n",
    "plt.close(fig)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import imageio\n",
    "import os\n",
    "\n",
    "png_dir = \"./frames/\"\n",
    "images = []\n",
    "for subdir, dirs, files in os.walk(png_dir):\n",
    "    files = sorted(files)\n",
    "    for file in files:\n",
    "        file_path = os.path.join(subdir, file)\n",
    "        if file_path.endswith(\".png\"):\n",
    "            images.append(imageio.imread(file_path))\n",
    "kargs = { 'duration': 2 }\n",
    "imageio.mimsave('./gif/ice_concentration.gif', images, **kargs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This GIF shows visualization of sea ice concentration data taken in the month of April in the years 2015 to 2018. One could clearly see the reduction in extent of sea ice between these years."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![Sea Ice concentration](./gif/ice_concentration.gif)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Evidence 3: Sea level rise"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The Global Mean Sea Level (GMSL) increases due to thermal expansion of water and by melting of ice sheets and glaciers on land \n",
    "\n",
    "The GMSL has risen by 10 to 20 centimeters over the past century and the annual rate of rise is at 3.2mm per year over the past 20years."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As Sea water level increases and reaches inland, it can cause erosion, more flooding, lost of habitat for fish, birds and plants. It also affects lives of millions of people who live in sea water flood prone low lying islands and other coastal regions."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Below you will see the data that illustrates changes in sea level since 1993 from NASA's satellite sea level observations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ftplib import FTP\n",
    "import re\n",
    "ftp_server = 'podaac.jpl.nasa.gov'\n",
    "directory = '/allData/merged_alt/L2/TP_J1_OSTM/global_mean_sea_level/'\n",
    "# Connecting to FTP server\n",
    "ftp = FTP(ftp_server)\n",
    "# Logging in to the ftp server (no login credentials required)\n",
    "ftp.login()\n",
    "# Changing the current working directory to the global mean sea level data directory\n",
    "ftp.cwd(directory)\n",
    "\n",
    "# Looping over all the existing files to find the GSML data file and reading it into a panda dataframe\n",
    "for f in ftp.nlst():\n",
    "    if re.match(r\"GMSL_TPJAOS(.*).txt\",f):\n",
    "        url = 'ftp://'+ftp_server+directory+f\n",
    "        print(\"Gathering data from\", url)\n",
    "        sea_level_df = pd.read_table(url, sep='\\s+', comment='H', header=None, usecols=[2,11], names=['Date','GMSL_variation'])\n",
    "ftp.quit();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import timedelta, datetime\n",
    "\n",
    "def convert_partial_year(number):\n",
    "\n",
    "    year = int(number)\n",
    "    d = timedelta(days=(number - year)*365)\n",
    "    day_one = datetime(year,1,1)\n",
    "    date = d + day_one\n",
    "    return date\n",
    "\n",
    "sea_level_df['Date'] = sea_level_df['Date'].apply(convert_partial_year)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sea_level_df.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "init_notebook_mode(connected=True)\n",
    "\n",
    "trace0 = go.Scatter(\n",
    "    x = sea_level_df['Date'],\n",
    "    y = sea_level_df['GMSL_variation']-sea_level_df['GMSL_variation'][1],\n",
    "    mode = 'lines',\n",
    "    name = 'Global Mean Sea level (GMSL) variation'\n",
    ")\n",
    "\n",
    "data = [trace0]\n",
    "layout = go.Layout(dict(title='Global Mean Sea level (GMSL) variation'),\n",
    "                   xaxis=dict(title='Year'),\n",
    "                   yaxis=dict(title='Sea height variation (mm)')\n",
    ")\n",
    "fig = go.Figure(data=data,layout=layout)\n",
    "iplot(fig)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Questions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Temperature Anomalies recorded in Canada:\n",
    "\n",
    "The graph below illustrates the Annual average temperature departures from the 1961-1990 reference value (or the average temperatures recorded between 1961 to 1990) in Canada from 1948 to 2014. The data was obtained from Government of Canada's Open Data Portal: https://open.canada.ca/data/en/dataset/49118c5d-850c-40bd-af75-4f47650a9800\n",
    "\n",
    "Take a look at the graph and answer the questions that follow."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ca_url = 'https://www.canada.ca/content/dam/eccc/migration/main/indicateurs-indicators/64c9e931-fcc6-4df6-ad44-c2b0236fe255/temperaturechange_en.csv'\n",
    "\n",
    "ca_df = pd.read_table(ca_url, sep=\",\", skiprows=[0,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "init_notebook_mode(connected=True)\n",
    "\n",
    "trace0 = go.Scatter(\n",
    "    x = ca_df['Year'],\n",
    "    y = ca_df['Temperature departure (degrees Celsius)'],\n",
    "    mode = 'lines+markers',\n",
    "    name = 'Annual Mean'\n",
    ")\n",
    "\n",
    "trace1 = go.Scatter(\n",
    "x = ca_df['Year'],\n",
    "y = [0]*len(ca_df['Temperature departure (degrees Celsius)']),\n",
    "mode = 'lines',\n",
    "#opacity = 0.5,\n",
    "name='Reference value',\n",
    "hoverinfo = 'none'\n",
    ")\n",
    "data = [trace0, trace1]\n",
    "layout = go.Layout(dict(title='Annual Average temperature departures in Canada'),\n",
    "                   xaxis=dict(title='Year'),\n",
    "                   yaxis=dict(title='Temperature Anomaly')\n",
    ")\n",
    "fig = go.Figure(data=data,layout=layout)\n",
    "iplot(fig)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. What was the warmest and coldest year in Canada and by how much was it above or below the reference value?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "_Type answers here._"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. Do you see a trend in the annual average temperature departure? Is it an upward or downward trend and what does it signify?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "_Type answers here._"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![alt text](https://github.com/callysto/callysto-sample-notebooks/blob/master/notebooks/images/Callysto_Notebook-Banners_Bottom_06.06.18.jpg?raw=true)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
